var toCode = function(str) {
	var key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var l = key.length;
	var a = key.split("");
	var s = "",
		b, b1, b2, b3;
	for (var i = 0; i < str.length; i++) {
		b = str.charCodeAt(i);
		b1 = b % l;
		b = (b - b1) / l;
		b2 = b % l;
		b = (b - b2) / l;
		b3 = b % l;
		s += a[b3] + a[b2] + a[b1];
	}
	return s;
}
var fromCode = function(str) {
	var key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var l = key.length;
	var b, b1, b2, b3, d = 0,
		s;
	s = new Array(Math.floor(str.length / 3));
	b = s.length;
	for (var i = 0; i < b; i++) {
		b1 = key.indexOf(str.charAt(d));
		d++;
		b2 = key.indexOf(str.charAt(d));
		d++;
		b3 = key.indexOf(str.charAt(d));
		d++;
		s[i] = b1 * l * l + b2 * l + b3
	}
	b = eval("String.fromCharCode(" + s.join(',') + ")");
	return b;
}

class Storage {
	constructor(keyValue) {
		this.key = keyValue;
	}
	get() {
		let value;
		try {
			return (value = window.localStorage.getItem(this.key)) &&
				typeof value !== 'undefined' ?
				JSON.parse(fromCode(value)) :
				undefined;
		} catch (err) {}
		return undefined;
	}
	set(data) {
		window.localStorage.setItem(this.key, toCode(JSON.stringify(data)));
	}
	del() {
		window.localStorage.removeItem(this.key);
	}
}


try {
	const PROJECT = require('@/settings').project;
} catch (error) {
	var PROJECT = '';
}
export const UserInfo = new Storage(`${PROJECT}_userInfo`);
export const Token = new Storage(`${PROJECT}_token`);